package com.yy.yyui.list
{
	import com.maqie.view.UM;
	import com.yy.yyui.DefaultItemRenderer;
	import com.yy.yyui.scroll.ScrollPanel;
	import com.yy.yyui.table.DataGrid;
	import com.yy.yyui.text.TextLabel;
	import com.util.tools.ArrayTools;
	import com.util.tools.GT;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextFormatAlign;
	
	public class List extends Sprite
	{
		public var scrollPanel:ScrollPanel;
		public var datagrid:DataGrid;
		private var w:Number,h:Number;
		
		private var _actions:Object;
		
		private var listBackground:DisplayObject;
		private var showCount:Number;
		private var _bgClass:Class;
		public function List(w:Number,h:Number,itemHeight:Number,showCount:Number, bgClass:Class=null)
		{
			//TODO: implement function
			super();
			this.w = w;
			this.h = h;
			this._bgClass = bgClass;
			this.showCount = showCount;
			listBackground = this.addChild(UM.scale9(new _bgClass()));
			GT.fulFill3(this,w,h,0,0);
			
			var srp:ScrollPanel;
			srp = new ScrollPanel(w,h,null,null,null,null,false,0);
			this.addChild(srp);
			this.scrollPanel = srp;
			
			
			//datagrid
			var dg:DataGrid;
			dg = new DataGrid(srp.contentScrollPanel.scrollRect.width,itemHeight);
			dg.enableRowMouseSelect = true;
			dg.cellPositions[0] = DataGrid.CENTER;
			this.scrollPanel.addItem(dg);
			this.datagrid = dg;
			
			//set action
			this.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
			this.addEventListener(MouseEvent.CLICK,mouseClickHandler);
			this._actions = {};
			this.onSelectItem = defaultOnSelectItem;
			this.onHoverItem = defaultOnHoverItem;
			this.datagrid.addEventListener(DataGrid.DATAGRID_UPDATE,srpUpdate);
			this.addEventListener(Event.REMOVED_FROM_STAGE,destroy);
		}
		
		public function destroy(...rest):void{
			this.removeEventListener(Event.REMOVED_FROM_STAGE,destroy);
			//			this.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
			//			this.removeEventListener(MouseEvent.CLICK,mouseClickHandler);
			this.datagrid.removeEventListener(DataGrid.DATAGRID_UPDATE,srpUpdate);
			GT.removeAllChild(this);
			GT.destroyActions(this.actions);	
		}
		
		private function srpUpdate(e:Event):void{
			this.scrollPanel.update();
		}
		
		private function mouseMoveHandler(e:MouseEvent):void{
			itemHandler(e,onHoverItem);
			//			var sx:Number = e.stageX;
			//			var sy:Number = e.stageY;
			//			var p:Point =this.datagrid.getRowAndColumnByXY(sx,sy);
			//			var previous:Object = nowHoverItem;
			//			var current:Object = p==null?null:p.y;
			//			if(previous!=current){
			//				this.onHoverItem(previous,current);
			//				nowHoverItem = current;
			//			}
			
			
		}
		
		private function mouseClickHandler(e:MouseEvent):void{
			itemHandler(e,onSelectItem);
			//			var sx:Number = e.stageX;
			//			var sy:Number = e.stageY;
			//			var p:Point =this.datagrid.getRowAndColumnByXY(sx,sy);
			//			var previous:Object = nowClickItem;
			//			if(p!=null){
			//				var current:Object = p.y;
			//				this.onSelectItem(previous,current);
			//				nowClickItem = current;
			//				
			//			}
			
		}
		
		private var nowClickItem:Object,nowHoverItem:Object;
		private function itemHandler(e:MouseEvent,action:Function):void{
			var sx:Number = e.stageX;
			var sy:Number = e.stageY;
			var p:Point =this.datagrid.getRowAndColumnByXY(sx,sy);
			var previous:Object;
			if(e.type == MouseEvent.CLICK){
				previous = nowClickItem;
			}else if(e.type == MouseEvent.MOUSE_MOVE){
				previous = nowHoverItem;
			}
			if(p!=null){
				var current:Object;
				//current = p.y;
				current = this.datagrid.getGrid(p.y,p.x);
				action(previous,current);
				if(e.type == MouseEvent.CLICK){
					nowClickItem = current;
				}else if(e.type == MouseEvent.MOUSE_MOVE){
					nowHoverItem = current;
				}
			}
		}
		
		public function clear():void{
			this.datagrid.clear();
			this.datagrid.removeAllBackground();
			this.datagrid.update();
		}
		
		
		
		public function defaultOnSelectItem(previousItem:Object,currentItem:Object):void{
		}
		
		public function defaultOnHoverItem(previousItem:Object,currentItem:Object):void{
		}
		
		public function get onSelectItem():Function{
			return this.actions.onSelectItem;
		}
		
		public function set onSelectItem(value:Function):void{
			this.actions.onSelectItem = value;
		}
		
		public function get onHoverItem():Function{
			return this.actions.onHoverItem;
		}
		
		public function set onHoverItem(value:Function):void{
			this.actions.onHoverItem = value;
		}
		
		public function get actions():Object
		{
			//TODO: implement function
			return this._actions;
		}
		
		public function set itemPosition(value:Object):void{
			var val:Object = value;
			if(val!=DataGrid.LEFT||val!=DataGrid.RIGHT){
				val = DataGrid.CENTER;
			}
			this.datagrid.cellPositions[0] = val;
			this.datagrid.update();
		}
		
		public function addItem(item:Object,position:String=TextFormatAlign.LEFT):DisplayObject{
			var row:uint = this.datagrid.cells.length;
			var dobj:DisplayObject;
			if(!(item is DisplayObject)){
				var dir:DefaultItemRenderer = new DefaultItemRenderer(this.datagrid.defaultCellWidth,this.datagrid.defaultCellHeight);
				var tl:TextLabel = new TextLabel("",this.datagrid.defaultCellWidth);
				tl.height = this.datagrid.defaultCellHeight;
				tl.textFormat.align = position;
				tl.text = item.toString();
				tl.x += 4;
				dir.content = tl;
				dobj = dir;
			}else{
				dobj = item as DisplayObject;
			}
			
			ArrayTools.setValue(this.datagrid.cells,dobj,row);
			var dg:DisplayObject = UM.scale9(new _bgClass());
			dg.width = this.datagrid.defaultCellWidth;
			this.datagrid.setRowBackground(row,dg);
			this.listBackground.width = this.datagrid.defaultCellWidth+2;
			this.listBackground.height = showCount*this.datagrid.defaultCellHeight;
			return dobj;
		}
		
		public function getItem(index:int):Object{
			return this.datagrid.getGrid(index,0);
		}
		
		public function removeItem(item:Object):void{
			var p:Point = this.datagrid.findGrid(item);
			if(p!=null){
				this.datagrid.deleteRow(p.y);
			}
		}
		
		public function removeItemByIndex(index:uint):void{
			this.datagrid.deleteRow(index);
			this.datagrid.update();
		}
		
		override public function get width():Number{return w;}
		override public function get height():Number{return h;}
		
		override public function set width(newWidth : Number) : void {
			var diff:Number = newWidth - w;
			var oldW:Number,oldH:Number;
			var i:uint,l:uint;
			this.scrollPanel.width += diff;			
			w = newWidth;
		}
		
		override public function set height(newHeight : Number) : void {
			var diff:Number = newHeight - h;
			var oldW:Number,oldH:Number;
			var i:uint,l:uint;
			this.scrollPanel.height += diff;			
			h = newHeight;			
		}		
		
	}
}